/**
 * Project		:	МойСПД
 * Created		:	12.09.2013
 * Author		:	Alexandr Tsvetkov
 */
package ua.at.tsvetkov.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Date string formatting.
 * 
 * @author Alexandr Tsvetkov
 */
public class DateUtil {

	private static final Locale				ru				= new Locale("RU-ru");

	private static final SimpleDateFormat	ddmmyy		= new SimpleDateFormat("dd.MM.yy", ru);
	private static final SimpleDateFormat	ddmmyyyy		= new SimpleDateFormat("dd.MM.yyyy", ru);
	private static final SimpleDateFormat	dd_mm_yy		= new SimpleDateFormat("dd-MM-yy", ru);
	private static final SimpleDateFormat	dd_mm_yyyy	= new SimpleDateFormat("dd-MM-yyyy", ru);

	/**
	 * @param date
	 * @return MM.DD.YY
	 */
	public static String getMmDdYy(Date date) {
		return ddmmyy.format(date);
	}

	/**
	 * @param date
	 * @return MM.DD.YYYY
	 */
	public static String getMmDdYyyy(Date date) {
		return ddmmyyyy.format(date);
	}

	/**
	 * @param date
	 * @return MM-DD-YY
	 */
	public static String getMm_Dd_Yy(Date date) {
		return dd_mm_yy.format(date);
	}

	/**
	 * @param date
	 * @return MM-DD-YYYY
	 */
	public static String getMm_Dd_Yyyy(Date date) {
		return dd_mm_yyyy.format(date);
	}

	/**
	 * Return is date between start and end dates
	 * 
	 * @param date
	 * @param start
	 * @param end
	 * @return true if date between start and end dates
	 */
	public static boolean isBetween(Date date, Date start, Date end) {
		return date.after(start) && date.before(end);
	}

	/**
	 * Returns the first date of the month
	 * 
	 * @param date
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date getFirstDateOfMonth(Date date) {
		return new Date(date.getYear(), date.getMonth(), 1);
	}

	/**
	 * Add one month to current date with year correction
	 * 
	 * @param date
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date addMonth(Date date) {
		if (date.getMonth() == 11)
			return new Date(date.getYear() + 1, 0, date.getDate());
		else
			return new Date(date.getYear(), date.getMonth() + 1, date.getDate());
	}

	/**
	 * Add months to current date with year correction
	 * 
	 * @param date
	 * @return
	 */
	public static Date addMonths(Date date, int count) {
		for (int i = 0; i < count; i++) {
			date = addMonth(date);
		}
		return date;
	}

	/**
	 * Substruct one month to current date with year correction
	 * 
	 * @param date
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date substractMonth(Date date) {
		if (date.getMonth() == 0)
			return new Date(date.getYear() - 1, 11, date.getDate());
		else
			return new Date(date.getYear(), date.getMonth() - 1, date.getDate());
	}

	/**
	 * Substruct months to current date with year correction
	 * 
	 * @param date
	 * @return
	 */
	public static Date substractMonths(Date date, int count) {
		for (int i = 0; i > count; i--) {
			date = substractMonth(date);
		}
		return date;
	}

	/**
	 * Change date for count months
	 * 
	 * @param date
	 * @param count can be negative or positive
	 * @return
	 */
	public static Date changeMonths(Date date, int count) {
		if (count > 0)
			return addMonths(date, count);
		else
			return substractMonths(date, count);
	}

}
